<template>
  <div>
    <el-dialog v-bind="$attrs" v-on="$listeners" :visible.sync="showDialog" @open="onOpen" @close="onClose" title="添加用户">
      <el-row :gutter="15">
        <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="80px">

          <el-col :span="12">
            <el-form-item label="部门编号" prop="dNum">
              <el-input v-model="formData.dNum" placeholder="请输入部门编号" clearable :style="{width: '100%'}">
              </el-input>
            </el-form-item>
            <el-form-item label="部门名称" prop="dName">
              <el-input v-model="formData.dName" placeholder="请输入部门名称" clearable :style="{width: '100%'}">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经理名称" prop="cName">
              <el-input v-model="formData.cName" placeholder="请输入经理名称" clearable :style="{width: '100%'}">
              </el-input>
            </el-form-item>
          </el-col>

        </el-form>
      </el-row>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import ImageCropper from '../../components/ImageCropper'
  import PanThumb from '../../components/PanThumb'

  export default {
    name: 'DepartmentAdd',
    components: {ImageCropper, PanThumb},
    // 接受父组件传递的值
    props:{
      addVisible:{
        type: Boolean,
        default: false
      },
    },
    data() {
      return {
        formData: {
          dNum: undefined,
          dName: undefined,
          cName: undefined,
        },
        // 控制弹出框显示隐藏
        showDialog:false,
        imagecropperShow: false, // 是否显示上传组件
        imagecropperKey: 0, // 上传组件id
        image: 'http://47.93.37.158:8888/group1/M00/00/00/rBklB2B6dKSAImyzAAIcU_dRVxE613.png',
        rules: {
          dNum: [{
            required: true,
            message: '请输入部门编号',
            trigger: 'blur'
          }]
          ,dName: [{
            required: false,
            message: '请输入部门名称',
            trigger: 'blur'
          }],
          cName: [{
            required: true,
            message: '请选择部门经理名称',
            trigger: 'blur'
          }],
        },
        field101Action: 'https://jsonplaceholder.typicode.com/posts/',
        field101fileList: [],
        sexOptions: [{
          "label": "帅哥",
          "value": 1
        }, {
          "label": "美女",
          "value": 2
        }],
      }
    },
    computed: {},
    watch: {
      // 监听 addOrUpdateVisible 改变
      addVisible(oldVal,newVal){
        this.showDialog = this.addVisible
      },
    },
    created() {},
    mounted() {},
    methods: {

      onOpen() {

      },
      onClose() {
        this.$refs['elForm'].resetFields()
        this.$emit('changeShow', 'false')
      },
      close() {
        this.$emit('changeShow', 'false')
      },
      handelConfirm() {
        this.$refs['elForm'].validate(valid => {
          if (!valid) return;
          this.$axios.post("/sys/department",this.formData).then((response) => {
          });
          this.close();
          location.reload();
        })
      },
      field101BeforeUpload(file) {
        let isRightSize = file.size / 1024 / 1024 < 2
        if (!isRightSize) {
          this.$message.error('文件大小超过 2MB')
        }
        let isAccept = new RegExp('image/*').test(file.type)
        if (!isAccept) {
          this.$message.error('应该选择image/*类型的文件')
        }
        return isRightSize && isAccept
      },
      // 上传成功后的回调函数
      cropSuccess(data) {
        console.log(data)
        var oldImg=this.image;
        this.imagecropperShow = false
        this.image = data.data;
        console.log(data.data)
        // 上传成功后，重新打开上传组件时初始化组件，否则显示上一次的上传结果
        this.imagecropperKey = this.imagecropperKey + 1;
        this.deleteAvatar(oldImg);
      },

      // 关闭上传组件
      closeImage() {
        this.imagecropperShow = false
        // 上传失败后，重新打开上传组件时初始化组件，否则显示上一次的上传结果
        this.imagecropperKey = this.imagecropperKey + 1
      },
      async deleteAvatar(oldImage){
        const {data} = await deleteImgFile(oldImage);
        console.log(data);
      }
    }
  }

</script>

<style scoped>

</style>
